#import <MachOFile.h>
/*
 * $Log:	MachO2.m,v $
Revision 1.1  94/02/07  21:36:18  ediger
Initial revision

 */

static char rcsident[] = "$Id: MachO2.m,v 1.1 94/02/07 21:36:18 ediger Exp Locker: ediger $";

void print_segments(id mach_o_file);
void print_threads(id oFile);

extern struct mach_header _mh_execute_header;

int
main(int ac, char **av)
{
	MachOFile *oFile;
	char buf[256];

	puts("Hit return to continue...");
	gets(buf);

	oFile = [[MachOFile alloc] init];

	[oFile considerMappedFiles];

	printf("address of header: 0x%x\n", &_mh_execute_header);
	[oFile fillFromAddress:(char *)&_mh_execute_header];

	print_threads(oFile);

	print_segments(oFile);

	[oFile free];

	puts("Hit return to finish...");
	gets(buf);

	return 0;
}

void
print_threads(oFile)
	id oFile;
{
	int iThreads, iCC;

	iThreads = [oFile threads];

	for (iCC = 0; iCC < iThreads; ++iCC)
	{	id            oThread = [oFile thread:iCC];
		unsigned long ulAddr = [oThread pc];

		printf("Thread's pc: 0x%lx\n", ulAddr);
	}
}

void
print_segments(oFile)
	id oFile;
{
	int iSegments, iCC;

	iSegments = [oFile mappedSegments];

	for (iCC = 0; iCC < iSegments; ++iCC)
	{	id              oSegment = [oFile mappedSegment:iCC];
		int             iSections;

		if ((iSections = [oSegment numberOfSections]) > 0)
		{	int             iC;

			for (iC = 0; iC < iSections; ++iC)
			{	struct section *sect = [oSegment getSection:iC];
				printf("\t%s in seg %s - 0x%lx, %lu bytes\n",
					sect->sectname, sect->segname,
					sect->addr, sect->size);
			}
		}

		printf("Segment 0x%lx => 0x%lx named \"%s\"\n",
			[oSegment getBaseAddress],
			[oSegment getUpperAddress],
			[oSegment commandName]);
	}

	return;
}
